package com.ruimei.service.impl;

import com.ruimei.entitys.CourseChange;
import com.ruimei.entitys.RmCourse;
import com.ruimei.entitys.RmCourseTime;
import com.ruimei.entitys.enums.LeaveStatus;
import com.ruimei.entitys.enums.Week;
import com.ruimei.mapper.CourseMapper;
import com.ruimei.service.CourseService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;

import com.ruimei.util.CommUtil;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class CourseServiceImpl
  implements CourseService
{
  @Resource
  CourseMapper courseMapper;
  
  public Map<String, Object> day(List<RmCourse> allData)
  {
    Map<Integer, String> day = new HashMap();
    Map<String, Object> map = new HashMap();
    day.put(Integer.valueOf(0), "MONDAY");
    day.put(Integer.valueOf(1), "TUESDAY");
    day.put(Integer.valueOf(2), "WEDNESDAY");
    day.put(Integer.valueOf(3), "THURSDAY");
    day.put(Integer.valueOf(4), "FRIDAY");
    day.put(Integer.valueOf(5), "SATURDAY");
    day.put(Integer.valueOf(6), "SUNDAY");
    List<RmCourseTime> timeList = this.courseMapper.getCourseTime();
    List<Object> list = new ArrayList();
    for (RmCourseTime t : timeList)
    {
      Map<String, Object> data = new HashMap();
      Map<String, Object> courseList = new HashMap();
      data.put("courseTime", t);
      for (RmCourse c : allData) {
        if (t.getTimeName().equals(c.getTimeName())) {
          for (int i = 0; i < day.size(); i++) {
            if (((String)day.get(Integer.valueOf(i))).equals(c.getWeek().toString())) {
              courseList.put(day.get(Integer.valueOf(i)), c);
            }
          }
        }
      }
      data.put("courseList", courseList);
      list.add(data);
    }
    map.put("status", Boolean.valueOf(true));
    map.put("message", "查询成功");
    map.put("count", Integer.valueOf(list.size()));
    map.put("data", list);
    
    return map;
  }

  @Override
  @Cacheable(value={"myCache"}, key="'getTeacherCoursebyteacherId'+ #teacherId")
  public Map<String, Object> getTeacherCoursebyteacherId(String teacherId)
  {
    List<RmCourse> allData = this.courseMapper.getTeacherCoursebyteacherId(teacherId);
    
    return day(allData);
  }

  @Override
  @Cacheable(value={"myCache"}, key="'getClassCourse'+ #classId")
  public Map<String, Object> getClassCourse(String classId)
  {
    List<RmCourse> allData = this.courseMapper.getStudentCourse(classId);
    
    return day(allData);
  }

  @Override
  public Map<String, Object> getOther(RmCourse rmCourse)
  {
    List<RmCourse> allData = this.courseMapper.getOther(rmCourse);
    List<RmCourse> teacherCouse = courseMapper.getTeacherCoursebyteacherId(rmCourse.getTeacherId());
    for (RmCourse t:teacherCouse){
      for (int c = 0; c<allData.size();c++){
        if( t.getWeek() == allData.get(c).getWeek() && t.getCourseTimeId().equals(allData.get(c).getCourseTimeId()) ){
          allData.remove(c);
        }
      }
    }

    return day(allData);
  }

  @Override
  public Map<String, Object> addChange(CourseChange courseChange) {
    Map<String,Object> map = new HashMap<>();
    if(courseChange.getStatus() == null){
      courseChange.setStatus(LeaveStatus.PROCESSING);
    }
    if(courseChange.getContent() == null){
      courseChange.setContent("");
    }
    if(courseChange.getApproval() == null){
      courseChange.setApproval("");
    }
    Integer i = courseMapper.addChange(courseChange);
    if (i.intValue() == 1)
    {
      map.put("status", Boolean.valueOf(true));
      map.put("message", "添加成功");
    }
    else
    {
      map.put("status", Boolean.valueOf(false));
      map.put("message", "添加失败");
    }
    return map;
  }

  @Override
  public Map<String, Object> setChange(CourseChange courseChange) {
    Map<String,Object> map = new HashMap<>();
    courseChange.setStatus(LeaveStatus.REPLYED);
    Integer i = courseMapper.setChange(courseChange);
    if (i.intValue() == 1)
    {
      map.put("status", Boolean.valueOf(true));
      map.put("message", "添加成功");
    }
    else
    {
      map.put("status", Boolean.valueOf(false));
      map.put("message", "添加失败");
    }
    return map;
  }

  @Override
  public Map<String, Object> getChange(CourseChange courseChange) {
    Map<String,Object> map = new HashMap<>();
    List<CourseChange> list = courseMapper.getChange(courseChange);
    map.put("status", Boolean.valueOf(true));
    map.put("message", "查询成功");
    map.put("count", Integer.valueOf(list.size()));
    map.put("data", list);

    return map;
  }

}


/* Location:              E:\360MoveData\Users\admin\Desktop\teacher_wap-0.0.1-SNAPSHOT.jar!\BOOT-INF\classes\com\ruimei\service\impl\CourseServiceImpl.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       0.7.1
 */